<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');  

/**
 * Controller for Modules API.
 * @package api/conrollers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2014
 */
class Module_api extends REST_Controller
{
    /*
     * Constructor Method
     */
    public function __construct() {
        parent::__construct();
        
        $this->load->model('module_model');
        
        $this->load->helper('api');
        $this->load->helper('module');
        
        $this->load->library('form_validation');
    }
    
    public function index_get()
    {
        $params = get_params();

        $modules = $this->module_model->get_api($params['args'], $params['fields'], $params['limit'], $params['offset'], $params['sort'], $params['order']);
        response_modules($modules);
    }
    
    public function index_post()
    {
        if( valid_add_module() )
        {
            $module = add_module();
            // if successful
            if( $module['id'] )
            {
                format_response('success', 201, 'Add Success', NULL, $module);
            } else
            {
                format_response('failed', 500, 'Server Error', array('database_error' => 'Database Error'));
            }
        } else
        {
            format_response('failed', 400, 'Invalid Parameter', $this->form_validation->error_array());
        }
    }

    
    public function index_put()
    {
        put_params();
        
        if( valid_edit_module() )
        {
            $module = edit_module();
            // if successful
            if( $module )
            {
                format_response('success', 200, 'Edit Success', NULL, $module);
            } else
            {
                format_response('failed', 500, 'Server Error', array('database_error' => 'Database Error'));
            }
        } else
        {
            format_response('failed', 400, 'Invalid Parameter', $this->form_validation->error_array());
        }
    }
    
    public function index_delete()
    {
        delete_params();
        
        if( valid_delete_module() )
        {
            $deleted = delete_module();
            if( $deleted === 'deleted' OR $deleted === 'archived' )
            {
                format_response('success', 200, 'Delete Success');
            } else
            {
                format_response('failed', 500, 'Server Error', array('database_error' => 'Database Error'));
            }
        } else
        {
            format_response('failed', 400, 'Invalid Parameter', $this->form_validation->error_array());
        }
    }

    public function module_by_id_get()
    {
        $params = get_params(4);

        $id = $this->uri->segment(3);
        
        $args = $params['args'];
        $args['id'] = $id;

        $modules = $this->module_model->get_api($args, $params['fields'], $params['limit'], $params['offset'], $params['sort'], $params['order']);
        response_modules($modules);
    }
}

/* End of file modules.php */
/* Location: ./application/modules/cms-modules/controllers/modules.php */